Completed
Push — master ( 66e16b...7e3e50 )
by Michael
06:17
created

script.js ➔ ... ➔ setTimeout   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
/**
2
 * Hide Prosemirror and show the default editor
3
 *
4
 * @param {string} text the wiki syntax to be shown in the textarea
5
 */
6
function showDefaultEditor(text) {
7
    window.Prosemirror.destroyProsemirror();
8
    window.proseMirrorIsActive = false;
9
    dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft);
0 ignored issues
show
Bug introduced by
The variable dw_locktimer seems to be never declared. If this is a global, consider adding a /** global: dw_locktimer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
    jQuery('#wiki__text').val(text).show();
11
    jQuery('#size__ctl').show();
12
    jQuery('.editBox > .toolbar').show();
13
}
14
15
/**
16
 * Hide the default editor and start a new Prosemirror Editor
17
 *
18
 * @param {string} json the prosemirror document json
19
 */
20
function showProsemirror(json) {
21
    const $textArea = jQuery('#wiki__text');
22
    const $prosemirrorJsonInput = jQuery('#dw__editform').find('[name=prosemirror_json]').val(json);
23
    try {
24
        window.Prosemirror.enableProsemirror();
25
    } catch (e) {
26
        console.error(e);
27
        let message = 'There was an error in the WYSIWYG editor. You will be redirected to the syntax editor in 5 seconds.';
28
        if (window.SentryPlugin) {
29
            SentryPlugin.logSentryException(e, {
0 ignored issues
show
Bug introduced by
The variable SentryPlugin seems to be never declared. If this is a global, consider adding a /** global: SentryPlugin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
30
                tags: {
31
                    plugin: 'prosemirror',
32
                    'id': JSINFO.id,
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
33
                },
34
                extra: {
35
                    'content': $textArea.val(),
36
                    'json': $prosemirrorJsonInput.val(),
37
                }
38
            });
39
            message += ' -- The error has been logged to Sentry.';
40
        }
41
        showErrorMessage(message);
42
        setTimeout(function() {
43
            jQuery('.plugin_prosemirror_useWYSIWYG').click();
44
        }, 5000);
45
    }
46
    window.proseMirrorIsActive = true;
47
    $textArea.hide();
48
    jQuery('#size__ctl').hide();
49
    jQuery('.editBox > .toolbar').hide();
50
51
    if (dw_locktimer.addField) {
0 ignored issues
show
Bug introduced by
The variable dw_locktimer seems to be never declared. If this is a global, consider adding a /** global: dw_locktimer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
        // todo remove this guard after the next stable DokuWiki release after Greebo
53
        dw_locktimer.init(dw_locktimer.timeout/1000, dw_locktimer.draft, 'prosemirror__editor');
54
        dw_locktimer.addField('input[name=prosemirror_json]');
55
    } else {
56
        console.warn('Draft saving in WYSIWYG is not available. Please upgrade your wiki to the current development snapshot.')
57
    }
58
}
59
60
/**
61
 * Initialize the prosemirror framework
62
 *
63
 * (This shouldn't do much until we actually use the editor, but we maybe shouldn't do this twice)
64
 */
65
function initializeProsemirror() {
66
    try {
0 ignored issues
show
Unused Code introduced by
Empty try statement found. This statement can be removed.
Loading history...
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
67
        /* DOKUWIKI:include lib/bundle.js */
68
    } catch (e) {
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
69
        const $textArea = jQuery('#wiki__text');
0 ignored issues
show
Bug introduced by
The variable $textArea seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$textArea.
Loading history...
70
        console.error(e);
71
        let message = 'There was an error initializing the WYSIWYG editor.';
0 ignored issues
show
Bug introduced by
The variable message seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.message.
Loading history...
72
        if (window.SentryPlugin) {
73
            SentryPlugin.logSentryException(e, {
74
                tags: {
75
                    plugin: 'prosemirror',
76
                    'id': JSINFO.id,
77
                },
78
                extra: {
79
                    'content': $textArea.val(),
80
                }
81
            });
82
            message += ' The error has been logged to sentry.';
83
        }
84
85
        showErrorMessage(message);
86
87
        DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', '');
88
    }
89
}
90
91
/**
92
 * Add the error message above the editor
93
 *
94
 * @param {string} errorMsg
95
 */
96
function showErrorMessage(errorMsg) {
97
    jQuery('.editBox').before(
98
        jQuery('<div class="error"></div>').text(errorMsg)
99
    );
100
}
101
102
jQuery(function () {
103
    window.proseMirrorIsActive = false;
104
    const $jsonField = jQuery('#dw__editform').find('[name=prosemirror_json]');
105
    if (!$jsonField.length) {
106
        // this is not an edit session
107
        return;
108
    }
109
110
    const $toggleEditorButton = jQuery('.plugin_prosemirror_useWYSIWYG');
111
    $toggleEditorButton.on('click', function() {
112
        const $textArea = jQuery('#wiki__text');
113
        jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', {
0 ignored issues
show
Bug introduced by
The variable DOKU_BASE seems to be never declared. If this is a global, consider adding a /** global: DOKU_BASE */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
114
            call: 'plugin_prosemirror_switch_editors',
115
            data: window.proseMirrorIsActive ? $jsonField.val() : $textArea.val(),
116
            getJSON: window.proseMirrorIsActive ? '0' : '1',
117
        }).done(function handleSwitchEditorResponse(data) {
118
            if (window.proseMirrorIsActive) {
119
                showDefaultEditor(data.text);
120
            } else {
121
                showProsemirror(data.json);
122
            }
123
        }).fail(function (jqXHR, textStatus, errorThrown) {
124
            console.error(jqXHR, textStatus, errorThrown); // FIXME: proper error handling
125
            if (jqXHR.responseJSON && jqXHR.responseJSON.error) {
126
                showErrorMessage(jqXHR.responseJSON.error);
127
            } else {
128
                let message = 'The request failed with an unexpected error.';
129
                if (window.SentryPlugin) {
130
                    SentryPlugin.logSentryException(new Error(textStatus), {
0 ignored issues
show
Bug introduced by
The variable SentryPlugin seems to be never declared. If this is a global, consider adding a /** global: SentryPlugin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
131
                        tags: {
132
                            plugin: 'prosemirror',
133
                            'id': JSINFO.id,
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
134
                            status: jqXHR.status
135
                        },
136
                        extra: {
137
                            'content': $textArea.val(),
138
                            'responseText': jqXHR.responseText,
139
                        }
140
                    });
141
                    message += ' -- The error has been logged to Sentry.';
142
                }
143
                showErrorMessage(message);
144
            }
145
        });
146
147
        const $current = DokuCookie.getValue('plugin_prosemirror_useWYSIWYG');
0 ignored issues
show
Bug introduced by
The variable DokuCookie seems to be never declared. If this is a global, consider adding a /** global: DokuCookie */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
148
        DokuCookie.setValue('plugin_prosemirror_useWYSIWYG', $current ? '' : '1');
149
    });
150
151
    initializeProsemirror();
152
153
    if (DokuCookie.getValue('plugin_prosemirror_useWYSIWYG')) {
0 ignored issues
show
Bug introduced by
The variable DokuCookie seems to be never declared. If this is a global, consider adding a /** global: DokuCookie */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
154
        showProsemirror($jsonField.val());
155
    }
156
});
157